from db.model import Model
#声明全局变量
Input_Username=0
Input_Password=0
def Login():
    global Input_Username
    Input_Username = input("请输入银行卡卡号：")
    if len(Input_Username)!=16:
        print("账户输入不合法，请确认后重新输入....")
        return
    print('')
    global Input_Password
    Input_Password = input("请输入六位用户密码：")
    if Passwork_Check(Input_Password)==False:
        print("密码输入不合法，请重新输入....")
        return
    if len(Input_Username)==16 and Passwork_Check(Input_Password)==True:
        print("校验通过")

'''定义密码格式校验函数'''
def Passwork_Check(P):
    if P.isdecimal()==False or len(P)!=6:
        return False
    return True
'''验证卡号与密码函数'''
def User_Pass_Check(Username,PassWord):
    print(m.find(Username)["Account"])
    print(m.find(Username)["Password"])
    if Username==m.find(Username)["Account"] and PassWord==m.find(Username)["Password"]:
        return True
    else:
        return False

'''校验输入的字符是否为数字'''
def Numbercheck(num):
    if num.isdecimal():#检验输入是否为数字
        return True
    return False

'''欢迎界面'''
def ATM_Welcome():
    print("="*72)
    print(" "*24,"欢迎使用逗比银行ATM机")
    print("="*72)
    print("*"*72)
    print("*"*30,"登录（1）","*"*31)
    print("*"*30,"退出（2）","*"*31)
    print("*"*72)
    Select_Features = input('请选择需要使用的功能，输入对应数字即可：')
    if Numbercheck(Select_Features) and int(Select_Features)>0 and 4>int(Select_Features):
        Select_Features=int(Select_Features)
        if Select_Features==1:
            Login()
        elif Select_Features==2:
            exit(0)

'''用户登录成功后操作界面的函数'''
def interface():
    print("="*11,"用户操作界面","="*12)
    print("{0:2} {1:12} {2:12} {3:12}".format("  ","1. 查询","2. 取款","3. 存款"),"\n")
    print("{0:2} {1:10} {2:12}".format("  ","4. 修改密码","5. 退出"),"\n")
    print("="*42)

'''用户账户信息查询函数'''
def use_Inquire(USEID):
    print("+"*15,"信息查询","+"*15)
    print("|{0:<4}|{1:<20}|{2:<10}|\r\n".format("账户名","卡号","余额(¥)"))
    print("|{0:<4}|{1:<20}|{2:<10}|\r\n".format(m.find(USEID)["Name"],m.find(USEID)["Account"],m.find(USEID)["Balance"]))

'''用户取款函数'''
def use_Withdrawal(USEID,Money_Out):
    MoneyNum=int(m.find(USEID)["Balance"])-int(Money_Out)
    if MoneyNum>=0:
        m.Updata({'Account':USEID,'Balance':MoneyNum})
        print("|{0:<4}|{1:<20}|{2:<10}|\r\n".format("账户名","卡号","余额(¥)"))
        print("|{0:<4}|{1:<20}|{2:<10}|\r\n".format(m.find(USEID)["Name"],m.find(USEID)["Account"],m.find(USEID)["Balance"]))
    else:
        print("账户余额不足！")

'''用户存款函数'''
def use_deposit(USEID,Money_Inpout):
    MoneyNum=int(m.find(USEID)["Balance"])+int(Money_Inpout)
    m.Updata({'Account':USEID,'Balance':MoneyNum})
'''用户密码修改函数'''
def Passwor_modify(USEID,OldPassword,New_password1,New_Password2):
    if OldPassword == m.find(USEID)["Password"]:
        if New_password1==New_Password2:
            if (m.Updata({'Account':USEID,'Password':New_password1}))!=None:
                print("密码已修改成功")
            return True
        else:
            print("两次输入密码不相同，密码修改失败！")
            return False
    else:
        print("旧密码输入错误！")
        return
login_Check=3#用户密码输入错误次数

if  __name__=='__main__':
    m = Model("users")
    ATM_Welcome()
    use_id = User_Pass_Check(Input_Username, Input_Password)
    print(use_id)
    if use_id!=None:
        print("欢迎使用逗比银行ATM机")
        while True:
            interface()
            OpenWork = input("请选择需要进行的操作：")
            print()
            if OpenWork == "1":
                print("+" * 20, "", "+" * 20)
                use_Inquire(Input_Username)
                input("按任意键返回上层..")
                print()
            elif OpenWork == "2":
                print("+" * 20, "取款界面", "+" * 20)
                Money_num = input("请输入取款金额：")
                print()
                use_Withdrawal(Input_Username, Money_num)
                use_Inquire(Input_Username)
                input("按任意键返回上层..")
                print()
            elif OpenWork == "3":
                print("+" * 20, "存款界面", "+" * 20)
                Money_num = input("请输入存款金额：")
                print()
                use_deposit(Input_Username, Money_num)
                use_Inquire(Input_Username)
                input("按任意键返回上层..")
                print()
            elif OpenWork == "4":
                print("+" * 20, "密码管理", "+" * 20)
                OldPassword = input("请输入旧密码：")
                print(OldPassword)
                print(m.find(Input_Username)["Password"])
                if Passwork_Check(OldPassword) and int(OldPassword) == int(m.find(Input_Username)["Password"]):
                    newpassword1 = input("请输入新密码：")
                    newpassword2 = input("请再次次输入新密码：")
                    if int(newpassword1) == int(newpassword2):
                        if (m.Updata({'Account': Input_Username, 'Password': newpassword1})) != None:
                            print("密码已修改成功")
                    else:
                        print("两次密码不同请重新输入..")
                else:
                    print("旧密码输入错误...")
                    break
            elif OpenWork == "5":
                print("*" * 30, "欢迎下次使用！", "*" * 30)
                break
            else:
                print("无此选项")
        else:
            if len(Input_Username) == 16:
                print(Input_Username)
                if (login_Check > 1):
                    print("密码错误请重新输入，您还有", login_Check - 1, "次机会")
                    login_Check -= 1
                else:
                    print("对不起，密码输入错误次数已达3次")
                    exit(0)
            else:
                print("回到初始界面，请重新选择。")
